/*** 弹窗 ***/
function openWindow(content){
	var windowDiv = document.createElement("div");
	windowDiv.id = "windowDiv";
	windowDiv.name = "windowDiv";
	windowDiv.style.position = "absolute";
	windowDiv.style.zIndex = "1000";
	windowDiv.style.top = "20px";
	//windowDiv.style.top = document.body.scrollTop + 20 + "px";
	windowDiv.style.left = "10px";
	windowDiv.style.right = "10px";
	windowDiv.style.height = document.documentElement.clientHeight - 90 + "px";
	windowDiv.style.padding = "50px 5px 5px 5px";
	windowDiv.style.backgroundColor = "#EEE";
	windowDiv.style.border = "1px solid #AAA";
	windowDiv.style.overflowY = "auto";
	windowDiv.style.wordBreak = "break-all";
	document.body.appendChild(windowDiv);
	document.getElementById("container").style.display = "none" ;
	//window.scrollTo( document.body.scrollLeft , parseFloat(windowDiv.style.top.split("p")[0]) - 20 );

	var backButton = document.createElement("input");
	backButton.id = "backButton";
	backButton.type = "button";
	backButton.style.position = "absolute";
	backButton.style.top = "5px";
	backButton.style.left = "5px";
	backButton.style.width = "60px";
	backButton.style.height = "30px";
	backButton.style.backgroundColor = "#1376D1";
	backButton.style.border = "1px solid #126AAA";
	backButton.style.lineHeight = "2.0em";
	backButton.style.fontSize = "12pt";
	backButton.style.color = "#FFF";
	backButton.align = "center";
	backButton.value = "返回";
	windowDiv.appendChild(backButton);

	var contentArea = document.createElement("div");
	contentArea.style.position = "absolute";
	contentArea.style.top = "45px";
	contentArea.style.left = "5px";
	contentArea.style.right = "5px";
	contentArea.style.height = document.documentElement.clientHeight - 100 + "px";
	contentArea.align = "center";
	contentArea.style.overflow = "auto";
	contentArea.innerHTML = content;
	//contentArea.style.border = "1px solid #333";
	windowDiv.appendChild(contentArea);

	document.getElementById("backButton").addEventListener("click",close,false);
	function close(){ document.body.removeChild(document.getElementById("windowDiv"));document.getElementById("container").style.display="inline"; }
}

/*** 判断移动端版本 ***/
var browser={
	versions:function(){
		   var u = navigator.userAgent, app = navigator.appVersion;
		   return {//移动终端浏览器版本信息
				trident: u.indexOf('Trident') > -1, //IE内核
				presto: u.indexOf('Presto') > -1, //opera内核
				webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
				gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
				mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), //是否为移动终端
				ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
				android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
				iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
				iPad: u.indexOf('iPad') > -1, //是否iPad
				webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
			};
		 }(),
		 language:(navigator.browserLanguage || navigator.language).toLowerCase()
}

/*** 创建Cookie ***/
function setCookie(name,value,expiredays){
	var date = new Date();
//	alert(date.getDate()+expiredays);
	date.setDate(date.getDate()+expiredays);
//	date.setTime(date.getTime() + (expiredays * 24 * 60 * 60 * 1000));
	
	document.cookie = name + "=" + escape(value) + ( ( expiredays == null ) ? "" : (";expires=" + date.toGMTString() + "; path=" + "/") );
}

/*** 读取Cookie ***/
function getCookie(name){
	if (document.cookie.length > 0)
	  {
		valueStart = document.cookie.indexOf(name + "=")//找到该cookie的起点
		if (valueStart != -1)
		{ 
			valueStart = valueStart + name.length + 1;
			valueEnd = document.cookie.indexOf(";",valueStart);
			if ( valueEnd == -1 ) valueEnd = document.cookie.length;
			return unescape(document.cookie.substring(valueStart,valueEnd));
		} 
	  }
	 else{ return ""; }
}

/*** 读取Cookie ***/
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 365 * 24 * 60 * 60 * 1000);
    if (getCookie(name)) {
        document.cookie = name + "=" + "; expires=" + exp + ";path=/";
    }
}

/*** 打开关闭指定对象 ***/
function openHiddenArea(element){
	var elementICON = element+"ICON";
	var element = document.getElementById(element);
	var elementICON = document.getElementById(elementICON);
	
	if ( element.style.display == "none" || element.style.display == "" )
	{
		element.style.display = "block";
		elementICON.className = "iconA";
	}
	else{
		element.style.display = "none";
		elementICON.className = "icon";
	}
}

/*** 打开关闭指定对象 ***/
function changeBackground(element,color,fontColor){
	var element = document.getElementById(element);
	element.style.backgroundImage = "none";
	element.style.backgroundColor = color;
	element.style.color = fontColor;
}

/*** 记录用户访问餐厅 ***/
/**
var link = window.location.href;
var visitedShop = "";
var visitedShopRecord = "";
if ( getCookie("shopVisited") )//如已有访问记录则在之前基础上新增
{
	if ( getCookie("shopVisited").indexOf("?") != -1 || getCookie("shopVisited").indexOf("&") != -1 )//如有以前不符合规范的Cookie则先清空，避免报错
	{ setCookie("shopVisited","",30) }
	var visitedShop = getCookie("shopVisited");
}
if ( link.indexOf("shop.php?id=") != -1 )//在普通预订餐厅主页进行记录
{
	if ( visitedShop.indexOf(window.location.search.split("id=")[1]) != -1 || visitedShop.indexOf(window.location.search.split("id=")[1].split("&")[0]) != -1 )
	{
		if( window.location.search.split("id=")[1].indexOf("&") != -1 )
			{ thisShopId = window.location.search.split("id=")[1].split("&")[0]; }
		else
			{ thisShopId = window.location.search.split("id=")[1]; }		
		visitedShop = visitedShop.replace(new RegExp(thisShopId+","), "");
		visitedShopRecord = thisShopId + "," + visitedShop;
	}
	else{
		if( window.location.search.split("id=")[1].indexOf("&") != -1 )
			{ visitedShop = window.location.search.split("id=")[1].split("&")[0] + "," + visitedShop; }
		else
			{ visitedShop = window.location.search.split("id=")[1] + "," + visitedShop; }
		if ( visitedShop.split(",").length > 8 ){ 
			for ( i=0 ; i <= 7 ; i++ )
			{
				visitedShopRecord += visitedShop.split(",")[i] + ",";
			} 
		}
		else{
			visitedShopRecord = visitedShop ;
		}
	}
	setCookie("shopVisited",visitedShopRecord,90);
}
else if ( link.indexOf("menu.php?") != -1 )//在餐厅预订点菜页面进行记录
{
	if ( visitedShop.indexOf(window.location.search.split("sid=")[1]) != -1 || visitedShop.indexOf(window.location.search.split("sid=")[1].split("&")[0]) != -1 )
	{
		if( window.location.search.split("sid=")[1].indexOf("&") != -1 )
			{ thisShopId = window.location.search.split("sid=")[1].split("&")[0]; }
		else
			{ thisShopId = window.location.search.split("sid=")[1]; }		
		visitedShop = visitedShop.replace(new RegExp(thisShopId+","), "");
		visitedShopRecord = thisShopId + visitedShop;
	}
	else{
		if( window.location.search.split("sid=")[1].indexOf("&") != -1 )
			{ visitedShop = window.location.search.split("sid=")[1].split("&")[0] + "," + visitedShop; }
		else
			{ visitedShop = window.location.search.split("sid=")[1] + "," + visitedShop; }
		if ( visitedShop.split(",").length > 8 ){ 
			for ( i=0 ; i <= 7 ; i++ )
			{
				visitedShopRecord += visitedShop.split(",")[i] + ",";
			} 
		}
		else{
			visitedShopRecord = visitedShop ;
		}
		setCookie("shopVisited",visitedShopRecord,90);
	}
}
else if ( link.indexOf("ExpressMenu.php?") != -1 )//在外卖点菜页面进行记录
{
	if ( visitedShop.indexOf(window.location.search.split("sid=")[1]) != -1 || visitedShop.indexOf(window.location.search.split("sid=")[1].split("&")[0]) != -1 )
	{
		if( window.location.search.split("sid=")[1].indexOf("&") != -1 )
			{ thisShopId = window.location.search.split("sid=")[1].split("&")[0]; }
		else
			{ thisShopId = window.location.search.split("sid=")[1]; }		
		visitedShop = visitedShop.replace(new RegExp(thisShopId+","), "");
		visitedShopRecord = thisShopId + visitedShop;
	}
	else{
		if( window.location.search.split("sid=")[1].indexOf("&") != -1 )
			{ visitedShop = window.location.search.split("sid=")[1].split("&")[0] + "," + visitedShop; }
		else
			{ visitedShop = window.location.search.split("sid=")[1] + "," + visitedShop; }
		if ( visitedShop.split(",").length > 8 ){ 
			for ( i=0 ; i <= 7 ; i++ )
			{
				visitedShopRecord += visitedShop.split(",")[i] + ",";
			} 
		}
		else{
			visitedShopRecord = visitedShop ;
		}
		setCookie("shopVisited",visitedShopRecord,90);
	}
}
else if ( link.indexOf("indRestaurantMenu.php?") != -1 )//在随手点点菜页面进行记录
{
	if ( visitedShop.indexOf(window.location.search.split("sid=")[1]) != -1 || visitedShop.indexOf(window.location.search.split("sid=")[1].split("&")[0]) != -1 )
	{
		if( window.location.search.split("sid=")[1].indexOf("&") != -1 )
			{ thisShopId = window.location.search.split("sid=")[1].split("&")[0]; }
		else
			{ thisShopId = window.location.search.split("sid=")[1]; }		
		visitedShop = visitedShop.replace(new RegExp(thisShopId+","), "");
		visitedShopRecord = thisShopId + visitedShop;
	}
	else{
		if( window.location.search.split("sid=")[1].indexOf("&") != -1 )
			{ visitedShop = window.location.search.split("sid=")[1].split("&")[0] + "," + visitedShop; }
		else
			{ visitedShop = window.location.search.split("sid=")[1] + "," + visitedShop; }
		if ( visitedShop.split(",").length > 8 ){ 
			for ( i=0 ; i <= 7 ; i++ )
			{
				visitedShopRecord += visitedShop.split(",")[i] + ",";
			} 
		}
		else{
			visitedShopRecord = visitedShop ;
		}
		setCookie("shopVisited",visitedShopRecord,90);
	}
}*/
//alert(getCookie("shopVisited"));

var addEvent=(function(){
	if(document.addEventListener){
		return function(el,type,fn){
			if(el.length){
				for(var i=0;i<el.length;i++){
					addEvent(el[i],type,fn);
				}
			}else{
				el.addEventListener(type,fn,false);
			}
		};
	}else{
		return function(el,type,fn){
			if(el.length){
				for(var i=0;i<el.length;i++){
					addEvent(el[i],type,fn);
				}
			}else{
				el.attachEvent('on'+type,function(){
					return fn.call(el,window.event);
				});
			}
		};
	}
})();
//浏览器兼容,通用的添加事件函数
/*function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);//DOM2.0
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);//IE5+
		return r;
	}
	else {
		elm['on' + evType] = fn;//DOM 0
	}
}*/

/** 四舍五入，保留小数点后两位 
*   Usage:  format_number(12345.678);
*   result: 12345.68
**/
function format_number(amount) {
    var i = parseFloat(amount);
    if(isNaN(i)) { i = 0.00; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}


/*** AJAX读取函数 ***/
function GetText(url, httpResponse){
	HTTP = new Array();
	HTTP._factories = [
		function() { return new XMLHttpRequest(); },
		function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
		function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
	];
	HTTP._factory = null;
	HTTP.newRequest = function() {
		if (HTTP._factory != null) return HTTP._factory();

		for(var i = 0; i < HTTP._factories.length; i++) {
			try {
				var factory = HTTP._factories[i];
				var request = factory();
				if (request != null) {
					HTTP._factory = factory;
					return request;
					alert(1);
				}
			}
			catch(e) {
				continue;
			}
		}
	HTTP._factory = function() {
			throw new Error("XMLHttpRequest not supported");
		}
		HTTP._factory(); // Throw an error
	}
	var request = HTTP.newRequest();
	request.onreadystatechange = function() {
		if (request.readyState == 4 ){  // If the request is finished
			if (request.status == 200)  // If it was successful
				httpResponse = request.responseText;  // Display the server's response
		}
	}
	request.open( "GET",url,false);
	request.send(null);
	return httpResponse;
}